import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    OnInit,
    ElementRef
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    TechFileInfoServiceProxy, ComboboxItemDto, TechFileInfoDto, TechFileInfoListDto,
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';
import * as moment from 'moment';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
    templateUrl: './techfileinfo.component.html',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./techfileinfo.component.less'],
    animations: [appModuleAnimation()]
})
export class TechFileInfosComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable1') dataTable1: Table;
    @ViewChild('paginator1') paginator1: Paginator;
    @ViewChild('stageTypeCombobox') stageTypeCombobox: ElementRef;
    @ViewChild('securityTypeCombobox') securityTypeCombobox: ElementRef;
    @ViewChild('techFileInfoTypeCombobox') techFileInfoTypeCombobox: ElementRef;

    primengTableHelper1 = new PrimengTableHelper();

    advancedFiltersAreShown = false;
    anyncing = false;
    filterText = '';

    stages: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();
    securitys: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();
    filetypes: Array<ComboboxItemDto> = new Array<ComboboxItemDto>();

    stage = -1;
    security = -1;
    filetype = -1;
    startFilterDate: Date;
    endFilterDate: Date;

    type = 1;

    constructor(
        injector: Injector,
        private _techfileServiceProxy: TechFileInfoServiceProxy,
        private _fileDownloadService: FileDownloadService,
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.getstage();
        this.getsecurity();
        this.getfiletype();
    }
    onUpload(event) {
        let list = event as Array<any>;

        let customerImportIntoList = list.map(el => {
            return {
                companyCodesStr: el.分发单位,
                drawingCode: el.图号,
                drawingName: el.图号名称,
                fileName: el.文件名称,
                modelName: el.型号,
                pageCount: el.页数,
                paragraphNo: el.部段号,
                remark: el.备注,
                security: el.密级,
                stage: el.阶段,
                techFileInfoTypeName: el.文件类型,
                alterationInvoiceCode: el.更改单号 };
            }) as Array<TechFileInfoListDto>;
        console.log(customerImportIntoList);
        this.message.confirm(
            '确认导入这' + list.length + '条数据吗？',
            this.l('AreYouSure'),
            isConfirmed => {
                if (isConfirmed) {
                    this.primengTableHelper.showLoadingIndicator();
                    this._techfileServiceProxy
                        .importTechFileList(customerImportIntoList)
                        .subscribe(result => {
                            this.getModels();
                            this.notify.success('导入数据成功');
                        });
                }
            }
        );
    }

    selectType(type: number) {
        this.type = type;
    }
    exportToExcel() {
        this._techfileServiceProxy.getTechFileInfosToExcel(
            this.filterText,
            this.filetype === -1 ? undefined : this.filetype,
            this.stage === -1 ? undefined : this.stage,
            this.security === -1 ? undefined : this.security,
            this.type === 1 ? true : false,
            this.startFilterDate ? moment(this.startFilterDate) : null,
            this.endFilterDate ? moment(this.endFilterDate) : null,
            this.primengTableHelper.getSorting(this.dataTable),
            1,
            1
        )
            .subscribe(result => {
                this._fileDownloadService.downloadTempFile(result);
            });
    }
    getstage() {
        this._techfileServiceProxy.getComboboxItemForStageType().subscribe(result => {
            this.stages = result;
            setTimeout(() => {
                $(this.stageTypeCombobox.nativeElement).selectpicker('refresh');
            });
        });
    }

    getsecurity() {
        this._techfileServiceProxy.getComboboxItemForSecurityType().subscribe(result => {
            this.securitys = result;
            setTimeout(() => {
                $(this.securityTypeCombobox.nativeElement).selectpicker('refresh');
            });
        });
    }

    getfiletype() {
        this._techfileServiceProxy.getComboboxItemForTechFileInfoType().subscribe(result => {
            this.filetypes = result;
            setTimeout(() => {
                $(this.techFileInfoTypeCombobox.nativeElement).selectpicker('refresh');
            });
        });
    }

    getModels(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._techfileServiceProxy
            .getTechFileInfos(
                this.filterText,
                this.filetype === -1 ? undefined : this.filetype,
                this.stage === -1 ? undefined : this.stage,
                this.security === -1 ? undefined : this.security,
                true,
                this.startFilterDate ? moment(this.startFilterDate) : null,
                this.endFilterDate ? moment(this.endFilterDate) : null,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    getModels1(event?: LazyLoadEvent) {
        if (this.primengTableHelper1.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper1.showLoadingIndicator();

        this._techfileServiceProxy
            .getTechFileInfos(
                this.filterText,
                this.filetype === -1 ? undefined : this.filetype,
                this.stage === -1 ? undefined : this.stage,
                this.security === -1 ? undefined : this.security,
                false,
                this.startFilterDate ? moment(this.startFilterDate) : null,
                this.endFilterDate ? moment(this.endFilterDate) : null,
                this.primengTableHelper1.getSorting(this.dataTable1),
                this.primengTableHelper1.getMaxResultCount(this.paginator1, event),
                this.primengTableHelper1.getSkipCount(this.paginator1, event)
            )
            .subscribe(result => {
                this.primengTableHelper1.totalRecordsCount = result.totalCount;
                this.primengTableHelper1.records = result.items;
                this.primengTableHelper1.hideLoadingIndicator();
            });
    }

    deleteModel(id: string) {
        this.message.confirm('确认删除该文件吗？', isConfirmed => {
            if (isConfirmed) {
                this._techfileServiceProxy.deleteTechFile(id).subscribe(() => {
                    this.reloadPage();
                    this.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }

    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
        this.paginator1.changePage(this.paginator1.getPage());
    }

    search() {
        this.paginator.changePage(this.paginator.getPage());
        this.paginator1.changePage(this.paginator1.getPage());
    }

}
